import { lazy } from 'react';
import type { RouteObject } from 'react-router-dom';

/**
 * 路由配置
 * @description 定义应用的所有路由配置，支持懒加载和代码分割
 */

// 懒加载页面组件
const HomePage = lazy(() => import('@/views/home'));
const StudentPage = lazy(() => import('@/views/student'));
const CoursePage = lazy(() => import('@/views/course'));
const SchedulePage = lazy(() => import('@/views/schedule'));
const SettingsPage = lazy(() => import('@/views/settings'));

/**
 * 路由路径常量
 */
export const ROUTE_PATHS = {
  /** 首页 */
  HOME: '/',
  /** 学生管理 */
  STUDENT: '/student',
  /** 课程管理 */
  COURSE: '/course',
  /** 课表管理 */
  SCHEDULE: '/schedule',
  /** 设置页面 */
  SETTINGS: '/settings'
} as const;

/**
 * 路由元信息接口
 */
export interface IRouteMetadata {
  /** 页面标题 */
  title: string;
  /** 是否需要认证 */
  requiresAuth?: boolean;
  /** 页面图标 */
  icon?: string;
  /** 是否在导航中显示 */
  showInNav?: boolean;
  /** 导航排序 */
  order?: number;
  /** 页面描述 */
  description?: string;
}

/**
 * 扩展的路由对象接口
 */
export interface IAppRouteObject extends Omit<RouteObject, 'children' | 'element'> {
  /** 路由元信息 */
  meta?: IRouteMetadata;
  /** 子路由 */
  children?: IAppRouteObject[];
  /** 路由组件 */
  element?: React.ComponentType<any>;
}

/**
 * 应用路由配置
 */
export const routes: IAppRouteObject[] = [
  {
    path: ROUTE_PATHS.HOME,
    element: HomePage,
    meta: {
      title: '首页',
      icon: 'HomeOutlined',
      showInNav: true,
      order: 1,
      description: '课程表概览和快速操作'
    }
  },
  {
    path: ROUTE_PATHS.STUDENT,
    element: StudentPage,
    meta: {
      title: '学生管理',
      icon: 'UserOutlined',
      showInNav: true,
      order: 2,
      description: '学生信息的增删改查管理'
    }
  },
  {
    path: ROUTE_PATHS.COURSE,
    element: CoursePage,
    meta: {
      title: '课程管理',
      icon: 'BookOutlined',
      showInNav: true,
      order: 3,
      description: '课程和分类的管理'
    }
  },
  {
    path: ROUTE_PATHS.SCHEDULE,
    element: SchedulePage,
    meta: {
      title: '课表管理',
      icon: 'CalendarOutlined',
      showInNav: true,
      order: 4,
      description: '课程安排和课表显示'
    }
  },
  {
    path: ROUTE_PATHS.SETTINGS,
    element: SettingsPage,
    meta: {
      title: '设置',
      icon: 'SettingOutlined',
      showInNav: true,
      order: 5,
      description: '应用设置和数据管理'
    }
  }
];

/**
 * 获取路由的元信息
 * @param path 路由路径
 * @returns 路由元信息
 */
export const getRouteMetadata = (path: string): IRouteMetadata | undefined => {
  const findRoute = (routeList: IAppRouteObject[]): IRouteMetadata | undefined => {
    for (const route of routeList) {
      if (route.path === path) {
        return route.meta;
      }
      if (route.children) {
        const childMeta = findRoute(route.children);
        if (childMeta) return childMeta;
      }
    }
    return undefined;
  };
  
  return findRoute(routes);
};

/**
 * 获取导航菜单路由
 * @returns 需要在导航中显示的路由列表
 */
export const getNavigationRoutes = (): IAppRouteObject[] => {
  return routes
    .filter(route => route.meta?.showInNav)
    .sort((a, b) => (a.meta?.order || 0) - (b.meta?.order || 0));
};

/**
 * 检查路由是否需要认证
 * @param path 路由路径
 * @returns 是否需要认证
 */
export const requiresAuthentication = (path: string): boolean => {
  const meta = getRouteMetadata(path);
  return meta?.requiresAuth || false;
};